

import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

import 'package:moonplay/common/app_common.dart';

class OnePage extends StatefulWidget {
  @override
  _OnePageState createState() => _OnePageState();
}

class _OnePageState extends State<OnePage> with SingleTickerProviderStateMixin {
  var _indexModel = [];
  Future _futureBuilderFuture;

  @override
  void initState() {
    super.initState();
    init();
  }

  @override
  void dispose() {
    super.dispose();
  }

  //初始化1
  init(){
    setState(() {
      _futureBuilderFuture = initGetIndex();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      appBar: _appBar(),
      body: commonFutureBuilder(
        context,
        future: _futureBuilderFuture,
        callWidget: bodyWidget(),
        callTap: init
      )
    );
  }

  Widget bodyWidget(){
    return CustomScrollView(
      reverse: false,
      slivers: _widgetLists()
    );
  }

  Widget _appBar(){
    return CustomAppbar(
      navigationBarBackgroundColor: Theme.of(context).bottomAppBarColor, 
      title: Container(
        padding: EdgeInsets.only(left: ScreenUtil().getWidthPx(30)),
        alignment: Alignment.centerLeft,
        child: Cptext(text: 'moonPlayer', style: TextStyle(color: Theme.of(context).hoverColor),),
      ),
      trailingWidget: Container(
        height: ScreenUtil().getWidthPx(88),
        child: Row(
          children: <Widget>[
            InkResponse(
              highlightColor: Colors.transparent,
              radius: 0.0,
              onTap: (){
                print('历史');
              },
              child: Container(
                width: ScreenUtil().getWidthPx(88),
                height: double.infinity,
                child: Icon(IconData(0xe74f, fontFamily: Constants.IconFontFamily),color: Theme.of(context).hoverColor, size: 20),
              ),
            ),
            InkResponse(
              highlightColor: Colors.transparent,
              radius: 0.0,
              onTap: (){
                print('搜索');
              },
              child: Container(
                width: ScreenUtil().getWidthPx(88),
                height: double.infinity,
                child: Icon(IconData(0xe752, fontFamily: Constants.IconFontFamily),color: Theme.of(context).hoverColor, size: 20),
              ),
            ),
          ],
        ),
      ), leadingWidget: null,
    );
  }

  Widget _widgetSwiper(){
    return Container(
      height: ScreenUtil().getWidthPx(460),
      child: Swiper(
        itemBuilder: (BuildContext context,int index){
          return new Image.network("http://via.placeholder.com/350x150",fit: BoxFit.fill,);
        },
        itemCount: 3,
      ),
    );
  }

  List<Widget> _widgetLists(){
    List<Widget> _list = [
      Container()
    ];
    // Widget _addSwiper = SliverStickyHeader(
    //   header: Container(),
    //   sliver: SliverList(
    //     delegate: SliverChildBuilderDelegate(
    //       (context, i) => Container(
    //         child: _widgetSwiper(),
    //       ),
    //       childCount: 1,
    //     ),
    //   ),
    // );
    // _list.add(_addSwiper);
    // for (var i = 0; i < _indexModel.length; i++) {
    //   Widget _addWidget = SliverStickyHeader(
    //     header: Container(
    //       height: ScreenUtil().getWidthPx(80),
    //       padding: EdgeInsets.only(left: ScreenUtil().getWidthPx(30)),
    //       color: Colors.white,
    //       child: Row(
    //         mainAxisAlignment: MainAxisAlignment.spaceBetween,
    //         children: <Widget>[
    //           Cptext(text: '${_indexModel[i]['columnName']}'),
    //           InkResponse(
    //             onTap: (){
    //               print('点击更多');
    //             },
    //             child: Container(
    //               width: ScreenUtil().getWidthPx(140),
    //               height: double.infinity,
    //               padding: EdgeInsets.only(right: ScreenUtil().getWidthPx(10)),
    //               alignment: Alignment.centerRight,
    //               child: Row(
    //                 mainAxisAlignment: MainAxisAlignment.end,
    //                 children: <Widget>[
    //                   Cptext(text: '更多', style: TextStyle(fontSize: 14, color: Theme.of(context).cardColor),),
    //                   Icon(IconData(0xe635, fontFamily: Constants.IconFontFamily),color: Theme.of(context).cardColor, size: 16),
    //                 ],
    //               ),
    //             ),
    //           )
    //         ],
    //       ),
    //     ),
    //     sliver: SliverGrid(
    //       gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
    //         //横轴元素个数
    //         crossAxisCount: 3,
    //         //纵轴间距
    //         mainAxisSpacing: 3.0,
    //         //横轴间距
    //         crossAxisSpacing: 3.0,
    //         //子组件宽高长度比例
    //         childAspectRatio: 0.6
    //       ),
    //       delegate: SliverChildBuilderDelegate(
    //         (context, idx) => Container(
    //           child: Stack(
    //             children: <Widget>[
    //               Container(
    //                 height: double.infinity,
    //                 decoration: BoxDecoration(
    //                   borderRadius: BorderRadius.all(Radius.circular(4))
    //                 ),
    //                 child: Column(
    //                   children: <Widget>[
    //                     Container(
    //                       height: ScreenUtil.getInstance().screenWidth / 3 / 0.75,
    //                       child: Stack(
    //                         children: <Widget>[
    //                           Container(
    //                             height: ScreenUtil.getInstance().screenWidth / 3 / 0.75,
    //                             decoration: BoxDecoration(
    //                               image: DecorationImage(
    //                                 image: NetworkImage(_indexModel[i]['resources'][idx]['thumbnailImage']),
    //                                 fit: BoxFit.cover,
    //                               ),
    //                               borderRadius: BorderRadius.all(Radius.circular(4))
    //                             ),
    //                           ),
    //                           Positioned(
    //                             bottom: 0,
    //                             left: 0,
    //                             right: 0,
    //                             child: Container(
    //                               height: ScreenUtil().getWidthPx(60),
    //                               alignment: Alignment.centerLeft,
    //                               padding: EdgeInsets.only(left: ScreenUtil().getWidthPx(10)),
    //                               color: Color.fromRGBO(0, 0, 0, 0.7),
    //                               child: Cptext(text: _indexModel[i]['resources'][idx]['tip'], style: TextStyle(color: Colors.white, fontSize: 14),),
    //                             )
    //                           )
    //                         ],
    //                       ),
    //                     ),
    //                     Expanded(
    //                       child: Container(
    //                         alignment: Alignment.center,
    //                         child: Cptext(text: _indexModel[i]['resources'][idx]['title'], style: TextStyle(fontSize: 14),),
    //                       ),
    //                     )
    //                   ],
    //                 ),
    //               ),
                  
    //             ],
    //           )
    //         ),
    //         childCount: 6,
    //       ),
    //     ),
    //   );
    //   _list.add(_addWidget);
    // }
    return _list;
  }

  /**
   * 网络请求
   */
  initGetIndex() async {
    String url = "http://v2.ms-wh.com/list/hot?pageSize=6&apuid=fd0dd5bbc00548f898b0032f7743b8cc&i=868600033380005&aid=0007efed1f536284&sid=unknown&hid=BKL*HONOR*HWBKL*kirin970*HUAWEIBKL-AL00*BKL-AL00*unknown&dName=BKL-AL00&version=1.5.1&bVer=1&bLevel=100&batteryS=3&bundleId=com.jaiscool.moonvideo&carrier=460030893488825&clientTime=1588776692497&l=zh_CN_%23Hans&locModel=BKL-AL00&sversion=9&loc=&locationS=0&net=2&plat=3&sor=hw";
    var _res = await HttpUtil().request(url, method: 'get', need_login: false);
    setState(() {
      _indexModel = _res;
    });
    if(_res != null) {
      return _res;
    } else {
      return null;
    }
  }
}
